﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using hospital_registration_system.BLL;
using hospital_registration_system.DAL;
using hospital_registration_system.Entity;

namespace hospital_registration_system.Utility
{
    /// <summary>
    /// 用户个人信息管理
    /// </summary>
    public class UserPatients
    {

        protected static PatientsService service = new PatientsService();
        protected static DepartmentsService departmentsService = new DepartmentsService();
        /// <summary>
        /// 通过用户的id查询挂号记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<MyMedicalRedcord> findMyMedicalRedcordByPatientsId(int id)
        {
            //先查询预约挂号表
            AppointmentDao appointmentDao = new AppointmentDao();
            Dictionary<string, object> appoin = new Dictionary<string, object>();
            appoin.Add("patient_id", id);
            //查询挂号记录
            List<Appointment> appointments = appointmentDao.selectByMap(appoin);
            //实例化需要的数据库查询层
            Clinic_timeDao clinic_TimeDao = new Clinic_timeDao();
            PatientsDao patientsDao = new PatientsDao();
            IllnessDao illnessDao = new IllnessDao();
            DepartmentsDao departmentsDao = new DepartmentsDao();
            DoctorDao doctorDao = new DoctorDao();
            //定义返回用的链表
            List<MyMedicalRedcord> mies = new List<MyMedicalRedcord>();
            //进行查询和封装
            try
            {
                foreach (Appointment ap in appointments)
                {
                    //封装记录id字段
                    MyMedicalRedcord redcord = new MyMedicalRedcord();
                    redcord.Id = ap.Id;
                    //封装预约时间字段
                    Clinic_time _Time = clinic_TimeDao.selectById(ap.Clinic_time_id);
                    redcord.Time = _Time.Treatment.ToString();
                    if (_Time.See_time.Equals(Clinic_time.SeeTime.T1))
                    {
                        redcord.Time += " 时段：9:00~12:00";
                    }
                    else
                    {
                        redcord.Time += " 时段：14:00~17:00";
                    }
                    //封装患者名字段
                    redcord.Patient_name = patientsDao.selectById(ap.Patient_id).Patient_name;
                    //封装疾病名字段
                    if (ap.Illness_id == -1)
                    {
                        redcord.Illness_name = "---";
                    }
                    else
                    {
                        redcord.Illness_name = illnessDao.select(ap.Illness_id).Illness_name;
                    }
                    //封装科室名字段
                    redcord.Department_name = departmentsDao.selectById(ap.Department_id).Department_name;
                    //封装医生名字段
                    redcord.Doctor_name = doctorDao.selectById(ap.Doctor_id).Doctor_name;
                    if (_Time.Treatment.Ticks < DateTime.Now.Ticks)
                    {
                        redcord.State = "已过期";
                    }
                    else
                    {
                        redcord.State = "未开始";
                    }
                    mies.Add(redcord);

                }
            }
            catch (NullReferenceException)
            {
                List<MyMedicalRedcord> m = new List<MyMedicalRedcord>();
                MyMedicalRedcord redcord = new MyMedicalRedcord();
                redcord.Id = -1;
                redcord.Illness_name = "无记录";
                redcord.Patient_name = "无记录";
                redcord.State = "无记录";
                redcord.Time = "无记录";
                m.Add(redcord);
                return m;
            }
            return mies;
        }

        public static string deleteMyMedicalRedcordByPatientsId(int id)
        {
            AppointmentDao dao = new AppointmentDao();
            Clinic_timeDao dao2 = new Clinic_timeDao();
            Appointment a = dao.selectById(id);//找到取消预约挂号信息，用于就诊时段计数-1
            if (dao.delete(id)&&dao2.countSub(a.Clinic_time_id))
            {
                return "删除成功";
            }
            else
            {
                return "删除失败";
            }

        }
        /// <summary>
        /// 用户查询所有科室信息
        /// </summary>
        /// <returns></returns>
        public static List<Departments> searchAllDepartments()
        {
            DepartmentsService departmentsService = new DepartmentsService();
            return departmentsService.selectAllDepartments();
        }
        

        public static List<Departments> searchDepartmentsByPatientId(int illsId)
        {
            return departmentsService.findDepartmentByIllnessId(illsId);
        }
    }
}
